package com.ys.service.storage.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.entity.base.TbFileInfo;
import com.ys.entity.flow.YspModel;
import com.ys.entity.storage.TccFbaPlan;
import com.ys.entity.storage.TccFbaPlanItem;
import com.ys.entity.storage.TccLogisticsSadFile;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.storage.TccFbaPlanEnum;
import com.ys.mapper.base.TbFileInfoMapper;
import com.ys.mapper.storage.TccLogisticsSadFileMapper;
import com.ys.service.storage.TccLogisticsSadFileService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2025-04-08
 * @Description: C88文件实现类
 * @Version: 1.0
 */
@Service("tccLogisticsSadFileService")
public class TccLogisticsSadFileServiceImpl implements TccLogisticsSadFileService {

    private static final Logger logger = LoggerFactory.getLogger(TccLogisticsSadFileServiceImpl.class);

    @Autowired
    private TccLogisticsSadFileMapper tccLogisticsSadFileMapper;

    @Autowired
    private TbFileInfoMapper tbFileInfoMapper;

    /**
     * 新增
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R add(Map<String, Object> params, Long menuId) throws Exception {

        //获取制单人信息
        TsUserChange tsUserChange = new TsUserChange().setUserId(ShiroUtils.getUserId()).setStatus(1).queryFirst();
        Long id = null;
        if(tsUserChange!=null){
            id= tsUserChange.getId();//制单人变更信息
        }else {
            return R.error("未查询到制单人信息！");
        }

        if (params.get("subData") != null) {
            JSONArray subData = JSON.parseArray(params.get("subData").toString());
            if (subData.size() == 0){
                return R.error("数据不允许为空!");
            }
            for (int i = 0; i < subData.size(); i++) {
                JSONObject jSONObject = subData.getJSONObject(i);
                if (jSONObject.getString("logInvoiceNo") == null) {
                    return R.error("发票号不允许为空！");
                }
                if (jSONObject.getString("fbaNo") == null) {
                    return R.error("FBA编号不允许为空！");
                }
                if (jSONObject.getString("logisticsService") == null) {
                    return R.error("物流服务商不允许为空！");
                }
                if (jSONObject.getString("vgsbm") == null) {
                    return R.error("运营公司编码不允许为空！");
                }
                if (jSONObject.getString("annexFile") == null) {
                    return R.error("C88文件附件不允许为空！");
                }
                //判断FBA不能重复
                if (new TccLogisticsSadFile().setFbaNo(jSONObject.getString("fbaNo")).count() != 0){
                    return R.error("FBA编号:"+ jSONObject.getString("fbaNo") + "重复不允许保存！");
                }
            }

            for (int i = 0; i < subData.size(); i++) {
                JSONObject jSONObject = subData.getJSONObject(i);
                TccLogisticsSadFile tccLogisticsSadFile = JSON.parseObject(JSON.toJSONString(jSONObject), TccLogisticsSadFile.class);
                tccLogisticsSadFile.setAnnexFile("1个附件");
                tccLogisticsSadFile.setDeclareState(0);
                tccLogisticsSadFileMapper.insert(tccLogisticsSadFile);

                //新增附件
                TbFileInfo tbFileInfo = TbFileInfo.tbFileInfo(46,tccLogisticsSadFile.getId(),"annexFile",tccLogisticsSadFile.getFbaNo() + "C88文件","pdf",
                        "pdf",0L,jSONObject.get("annexFile").toString(),id,1);
                tbFileInfoMapper.insert(tbFileInfo);
            }
        }else {
            return R.error("数据不允许为空!");
        }

        return R.ok(AddDto.closeAndReload());
    }
}
